import type { ResponsePaginationData} from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { OwnerFinance, OwnerFinanceListItem, QueryListParams } from './data';
import { message } from 'antd';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';

/**
 * 获取流水列表
 * @param params
 */
export async function queryOwnerFinancePageList(
  params: QueryListParams,
): Promise<RequestData<OwnerFinanceListItem>> {
  const queryListParams: QueryListParams = {
    id: params.id,
    page: params.page,
    pageSize: params.pageSize,
    ownerName: params.ownerName,
    userType: params.userType,
  };
  const res: ResponsePaginationData<OwnerFinance> = await request(`/finance/owner`, {
    params: queryListParams,
  });

  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

export async function exportAllOwner() {
  const res = await request('/finance/owner/export');
  if (res.code === SUCCESS_CODE) {
    return { success: true, message: '请求成功', downloadUrl: res.data.downloadUrl };
  }
  return { success: false, message: res.message || '请求失败', downloadUrl: '#' };
}
